package common

import (
	"net"
	"time"
)

// WrapperTcpWithTimeout 包装TCP连接的创建，并添加超时
func WrapperTcpWithTimeout(network, address string, timeout time.Duration) (net.Conn, error) {
	// 创建一个Dialer结构体并设置超时
	d := &net.Dialer{Timeout: timeout}
	return WrapperTCP(network, address, d)
}

func WrapperTCP(network, address string, forward *net.Dialer) (net.Conn, error) {
	var conn net.Conn
	var err error
	// 拨号连接到指定网络上的地址。
	conn, err = forward.Dial(network, address)
	if err != nil {
		return nil, err
	}
	return conn, nil
}

//dailer, err := Socks5Dailer(forward)
//if err != nil {
//return nil, err
//}
//conn, err = dailer.Dial(network, address)
//if err != nil {
//return nil, err
//}

//func Socks5Dailer(forward *net.Dialer) (proxy.Dialer, error) {
//	u, err := url.Parse(Socks5Proxy)
//	if err != nil {
//		return nil, err
//	}
//	if strings.ToLower(u.Scheme) != "socks5" {
//		return nil, errors.New("Only support socks5")
//	}
//	address := u.Host
//	var auth proxy.Auth
//	var dailer proxy.Dialer
//	if u.User.String() != "" {
//		auth = proxy.Auth{}
//		auth.User = u.User.Username()
//		password, _ := u.User.Password()
//		auth.Password = password
//		dailer, err = proxy.SOCKS5("tcp", address, &auth, forward)
//	} else {
//		dailer, err = proxy.SOCKS5("tcp", address, nil, forward)
//	}
//
//	if err != nil {
//		return nil, err
//	}
//	return dailer, nil
//}
